/**
 * Created by meiguizhinian on 2018/7/20
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIEditBox = ivy.UIBase.extend({
	ctor: function (formName, blockName, sourceControlData) {
		this._super();
		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},

	initWith : function(formName, blockName, sourceControlData) {
		this.init();
		let uim = ivy.UIManager.getInstance();
		let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
		let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
		let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
		let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
		let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
		this._layerIDInEditor = layerID;

		let backImagePath = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_BackgroundImagePath_string]);

		if (!this._editBox && backImagePath.length>0)
		{
			this._editBox = new cc.EditBox(cc.size(w,h), new cc.Scale9Sprite(backImagePath));
			let defaultText= uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_DefaultText_string]);
			let placeHoldText= uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_PlaceHoldText_string]);
			let textColor = ivy.Tools.getColor4BFromInt32(uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_TextColor_color]));
			let placeHoldTextColor = ivy.Tools.getColor4BFromInt32(uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_PlaceHoldTextColor_color]));
			let textSize = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_TextSize_int]);
			let placeHoldTextSize = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_PlaceHoldTextSize_int]);
			let maxLength = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_MaxLength_int]);
			let inputMode = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_InputMode_list]);
			let inputFlag = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_InputFlag_list]);
			let keyboardReturnType = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_EditBox_KeyboardReturnType_list]);

			this._editBox.setString(defaultText);
			this._editBox.setPlaceHolder(placeHoldText);
			this._editBox.setFontColor(textColor);
			this._editBox.setPlaceholderFontColor(placeHoldTextColor);
			this._editBox.setFontSize(textSize);
			this._editBox.setPlaceholderFontSize(placeHoldTextSize);
			if (maxLength > 0)
				this._editBox.setMaxLength(maxLength);
			this._editBox.setInputMode(inputMode);
			this._editBox.setInputFlag(inputFlag);
			this._editBox.setReturnType(keyboardReturnType);
			this.addChild(this._editBox);
		}

		this.setSrcRect(cc.rect(x, y, w, h));
		this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
		this.setSrcPosition(this.getPosition());
		this.setContentSize(cc.size(w, h));
		let anchorType = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_Anchor_list]);
		this.setAnchorType(anchorType);
		this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_EnterAction_action]));
		this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_OutAction_action]));
		this.setUIElementName(formName, blockName);

		//////////////////////////////////////////////////////////////////////////
		//设置初始可视性 wsq-2017-3-7
		let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
		if (isNotVisble === 1)
		{
			this.setVisible(false);
		}
	},

	setAnchorType : function(type) {
		this._super(type);
		let rect;
		let size;
		if (!this.getParent()) {
			rect = cc.rect(this.getSrcRect());
			size = cc.size(rect);
		} else {
			rect = cc.rect(this.getSrcRect());
			size = cc.size(rect);
			let parent = this.getParent();
			if (parent instanceof ivy.UIBase) {
				let rect1 = parent.getSrcRect();
				if (rect1.x===0 && rect1.y===0 && rect1.width === 0 && rect1.height === 0) {
				} else {
					let pos22 = cc.p(rect1.x + rect1.width / 2, rect1.y + rect1.height / 2);
					rect.x  -= pos22.x;
					rect.y  -= pos22.y;
					rect.y = -rect.y;
					let ppp = ivy.Tools.ConvertPointFromGL(cc.p(rect));
					rect.x = ppp.x;
					rect.y = ppp.y;
				}
			}
		}

		this.setContentSize(size);
		switch (type) {
			case ivy.UIBase.eAnchorLT://ok
				if (this._editBox)
					this._editBox.setPosition(cc.p(size.width / 2.0, -size.height / 2.0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x, rect.y)));
				break;
			case ivy.UIBase.eAnchorLB://ok
				if (this._editBox)
					this._editBox.setPosition(cc.p(size.width / 2.0, size.height / 2.0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x, rect.y + size.height)));
				break;
			case ivy.UIBase.eAnchorRT://ok
				if (this._editBox)
					this._editBox.setPosition(cc.p(-size.width / 2.0, -size.height / 2.0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width, rect.y)));
				break;
			case ivy.UIBase.eAnchorRB://ok
				if (this._editBox)
					this._editBox.setPosition(cc.p(-size.width / 2.0, size.height / 2.0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width, rect.y + size.height)));
				break;
			case ivy.UIBase.eAnchorHT://ok
				if (this._editBox)
					this._editBox.setPosition(cc.p(0, -size.height / 2.0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y)));
				break;
			case ivy.UIBase.eANchorHB://ok
				if (this._editBox)
					this._editBox.setPosition(cc.p(0, size.height / 2.0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y + size.height)));
				break;
			case ivy.UIBase.eAnchorLV://ok
				if (this._editBox)
					this._editBox.setPosition(cc.p(size.width / 2.0, 0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x, rect.y + size.height / 2.0)));
				break;
			case ivy.UIBase.eAnchorRV://ok
				if (this._editBox)
					this._editBox.setPosition(cc.p(-size.width / 2.0 + rect.width / 2.0, 0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + rect.width / 2.0, rect.y + size.height / 2.0)));
				break;
			case ivy.UIBase.eAnchorHV://ok
			default:
				if (this._editBox)
					this._editBox.setPosition(cc.p(0, 0));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y + size.height / 2.0)));
				break;
		}
		this.setSrcPosition(this.getPosition());
	},
	getControl : function() {
		return this._editBox;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	_editBox: null,//ccui.EditBox.
});

ivy.UIEditBox.create = function (formName, blockName, sourceControlData) {
	return new ivy.UIEditBox(formName, blockName, sourceControlData);
};
